<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Item_Item extends Controller_User_Template {
	
	public function action_index()
	{
		$this->template->title = __('Sporting Goods Company');		
		$this->template->content = View::factory('items/item');
	}
	
	public function action_viewcategory()
	{
		$page = $this->request->param('id');
		$category_id = $_GET['subcategory'];
		
		$category = ORM::factory('category')
				->where('id' ,'=', $category_id)
				->find();
		
		if($category->parent_category_id == 0):
			$items = array();
			foreach($category->subcategories->find_all() as $subcats):
				foreach($subcats->items->find_all() as $item):
					$items[] = $item;
				endforeach;
			endforeach;
			
			$itemcount = count($items);
		else:
			$items = ORM::factory('item')
				->where('category_id', '=', $category_id)
				->and_where('discontinued', '=', 0)
				->find_all();
			$itemcount = count($items);
		endif;	
		
		
			
		$view = new View('items/viewitems');
		$view->set("title", $category->name);
		$view->set("items", $items);
		$view->set("page", $page);
		$view->set('action', 'viewcategory');
		$view->set("itemcount", $itemcount);
		
		$this->template->content = $view;
	}
	
	public function action_search()
	{
		$page = $this->request->param('id');
		$keyword = $_GET['keyword'];
		
		$items = ORM::factory('item')
			->where('name', '=', $keyword)
			->and_where('discontinued', '=', 0)
			->order_by('name', 'asc')
			->find_all();
			
		$itemcount = count($items);
			
		$view = new View('items/viewitems');
		$view->set("title", 'Search: '.$keyword);
		$view->set("items", $items);
		$view->set("page", $page);
		$view->set("itemcount", $itemcount);
		$view->set('action', 'search');
		
		$this->template->content = $view;
	}
	
	public function action_promotion() {
		$page = $this->request->param('id');
		$promotion_id = $_GET['promotion'];
		
		$promotion = ORM::factory('promotion', $promotion_id);
		
		$items = $promotion
			->items
			->where('discontinued', '=', 0)
			->order_by('name', 'asc')
			->find_all();	
			
		$itemcount = count($items);
			
		$view = new View('items/viewitems');
		$view->set("title", $promotion->description);
		$view->set("items", $items);
		$view->set("page", $page);
		$view->set('action', 'promotion');
		$view->set("itemcount", $itemcount);
		
		$this->template->content = $view;
		
	}
	
	public function action_allpromotions() {
		$page = $this->request->param('id');
		
		$promotions = ORM::factory('promotion')
			->where('starting_date', '<', strtotime('now'))
			->where('ending_date', '>', strtotime('now'))
			->find_all();
		
		$items = array();
		
		foreach($promotions as $promotion) {
			foreach($promotion->items->find_all() as $prom_item) {
				$items[] = $prom_item;	
			}	
		}
			
		$itemcount = count($items);
			
		$view = new View('items/viewitems');
		$view->set("title", 'All Special Promotions');
		$view->set("items", $items);
		$view->set("page", $page);
		$view->set('action', 'allpromotions');
		$view->set("itemcount", $itemcount);
		
		$this->template->content = $view;
			
	}
	
	public function action_viewitems()
	{
		$page = $this->request->param('id');
		
		$items = ORM::factory('item')
				->find_all();
				
		$itemcount = count($items); // returns the number of entries in table, useful for stopping pagination
		
		$view = new View('items/viewitems');
		$view->set('title', 'All Items');
		$view->set("items", $items);
		$view->set("page", $page);
		$view->set("itemcount", $itemcount);
		$view->set('action', 'viewitems');
		
		$this->template->content = $view;
	}
	
	public function action_sitem()
	{
		$item_id = $this->request->param('id');
		
		$item = new Model_Item($item_id);
		
		$attributes = $item->item_attributes->find_all();
			
		$view = View::factory('items/item');
		$view->set("item", $item);
		$view->set("attributes", $attributes);
		
		$this->template->title = 'Item';
		$this->template->content = $view;
	}
}